import falcon
import jenkins
from wsgiref import simple_server


class JenkinsActions(object):
    def __init__(self):
        self.server = jenkins.Jenkins('http://119.254.155.104:8000',
                                      username='suo',
                                      password='rachel')

    def on_post(self, req, resp):
        job_name = req.params.get('job_name')
        if job_name:
            try:
                self.server.build_job(job_name)
            except jenkins.NotFoundException:
                resp.body = ("Job %s not found\n" % job_name)

    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200
        resp.body = ('\nTest Jenkins Server\n')


application = falcon.API()
jenkins_action =  JenkinsActions()
application.add_route('/jenkins', jenkins_action)


if __name__ == '__main__':
    httpd = simple_server.make_server('0.0.0.0', 8001, application)
    print "Serving at 0.0.0.0:8001..."
    httpd.serve_forever()
